//
//  ViewController.m
//  14-UICollectionView
//
//  Created by dllo on 16/8/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"
#define  kCollectionViewCellReuse @"reuse"
#define  kCollectionViewReusable @"reusable"
@interface ViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //1.UICollectionView
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
   //1.itemSize 默认大小 50， 50
    layout.itemSize = CGSizeMake(CGRectGetWidth(self.view.bounds) / 2 - 7.5, 120);
    //2.scrollDirection滚动方向  UICollectionViewScrollDirectionVertical 竖直滚动,（默认）
     //UICollectionViewScrollDirectionHorizontal 水平滚动
     //滚动方向改变， 会交换列和行 (区)
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //3.minimumLineSpacing 最小行间距
    layout.minimumLineSpacing = 5;
    //4.minimumInteritemSpacing 最小列间距
    layout.minimumInteritemSpacing = 5;
    //5.sectionInset
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
   //6.headerReferenceSize  区头大小 横向滚动时， 设置宽； 纵向滚动时设置高
    layout.headerReferenceSize = CGSizeMake(100, 100);
    //7.footerReferenceSize 区尾大小 横向滚动时， 设置宽； 纵向滚动时设置高

    layout.footerReferenceSize = CGSizeMake(200, 200);
    //8.区头悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
    //8.区尾悬浮效果
    layout.sectionFootersPinToVisibleBounds = YES;
   //UICollectionView 创建
   //参数1.frame
   //参数2.填入不能为空的layout（布局）
    UICollectionView *myCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    myCollectionView.delegate = self;
    myCollectionView.dataSource = self;
    [self.view addSubview:myCollectionView];
    [myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:kCollectionViewCellReuse];
    
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable];
    
    [myCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusable];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 10;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor greenColor];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewReusable forIndexPath:indexPath];
    reusableView.backgroundColor = [UIColor redColor];
        return reusableView;
    } else {
        UICollectionReusableView *reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kCollectionViewReusable forIndexPath:indexPath];
        reusableView.backgroundColor = [UIColor blueColor];
        return reusableView;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
